﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using GameSPK.DAO;
using GameSPK.Model;
using GameSPK.Helpers;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;


namespace GameSPK.Start
{
    class frmGioiThieuPhanThi: frmGenerel
    {
        private static frmGioiThieuPhanThi instance;
        private static object syncLock = new object();
        
        public static frmGioiThieuPhanThi getInstance()
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmGioiThieuPhanThi();
                        
                    }
                }
            }

            return instance;
        }
        private frmGioiThieuPhanThi()
            : base()
        {

            this.Closed += new EventHandler(frmGioiThieuPhanThi_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmGioiThieuPhanThi_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (System.Windows.Input.KeyEventHandler)frmGioiThieuPhanThi_KeyDown);
            
        }
        List<PhanThi_CuocThi> phanthichon;
        void frmGioiThieuPhanThi_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            phanthichon = PhanThiDAO.getPhanThiChon(MainWindow.getCuocThiChon());
            if (phanthichon == null)
                return;
            Generate();

        }
        void Generate()
        {
            //button back
            Control control1;
            Button btnBack = new Button();
            control1 = (Control)btnBack;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML("PhanChoiLayout.xml", "ButtonBack");
            AssignControl.assignControlLayout(ref control1, lableXML, "btnBack");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getBackButtonUrl(), UriKind.RelativeOrAbsolute));
            //this.Background = myBrush;
            btnBack.Background = myBrush;
            btnBack.Click += new RoutedEventHandler(btnBack_Click);
            for (int i = 0; i < phanthichon.Count; i++)
            {
                Control control2;
                Label lblPhanThi;
                lblPhanThi = new Label();
                control2 = (Control)lblPhanThi;
                PhanThi phanthi = PhanThiDAO.getPhanThi(phanthichon[i].MaPT);
                lableXML = Helper.getLabelFromXML("PhanChoiLayout.xml", "PhanThi" + i,"PhanThis");
                AssignControl.assignControlLayout(ref control2, lableXML, "phanthi"+phanthichon[i].MaPT);
                lblPhanThi.Foreground = lableXML.Foreground;
                System.Windows.Controls.TextBlock contenLable = new System.Windows.Controls.TextBlock();
                //contenLable.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //contenLable.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                contenLable.TextWrapping = System.Windows.TextWrapping.WrapWithOverflow;
                contenLable.Text = phanthi.TenPT.Trim();
                lblPhanThi.Content = contenLable;
                lblPhanThi.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                lblPhanThi.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                lblPhanThi.FontSize = lableXML.FontSize;
                lblPhanThi.FontWeight = System.Windows.FontWeights.Bold;
                lblPhanThi.Style = (Style)this.FindResource("PulseLabel");
                DrawingBrush myDrawingBrush = new DrawingBrush();
                GeometryDrawing myGeometryDrawing = new GeometryDrawing();
                myGeometryDrawing.Brush = lableXML.Background;

                myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
                GeometryGroup ellipses = new GeometryGroup();
                ellipses.Children.Add(new EllipseGeometry(new Point(5, 5), 25, 25));
                myGeometryDrawing.Geometry = ellipses;
                myDrawingBrush.Drawing = myGeometryDrawing;
                lblPhanThi.Background = myDrawingBrush;
                lblPhanThi.MouseDoubleClick += new MouseButtonEventHandler(lblPhanThi_MouseDoubleClick);

                gridMain.Children.Add(lblPhanThi);
            }
            
            gridMain.Children.Add(btnBack);

        }

        void btnBack_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void lblPhanThi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            Label phanthilbl = (Label)sender;
            string mapt = phanthilbl.Name.Replace("phanthi", "");
            if (mapt == "3")
            {
                frmOChuStart frmOchu = frmOChuStart.getInstance();
                frmOchu.ShowDialog();
                frmOchu.Focus();
                return;
            }
            frmChonCauHoi choncauhoi = frmChonCauHoi.getInstance(int.Parse(mapt));
            choncauhoi.ShowDialog();
            choncauhoi.Focus();
        }
        void frmGioiThieuPhanThi_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        }
        void frmGioiThieuPhanThi_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
    }
}
